<?php
/**
 * Created by PhpStorm.
 * User: xshaitt
 * Date: 2016/8/3
 * Time: 14:13
 */
namespace Home\Controller;

use Think\Controller;

class UpdateController extends Controller
{
    public function dpUpload()
    {
        $uid = I('session.qzone')['home']['id'];
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/uploads/'; // 设置附件上传根目录
        $upload->subName = array('date', 'Ymd'); // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息

            $this->ajaxReturn(array('status' => 1,'message'=>'上传失败'));
        } else {// 上传成功
            $path = $upload->rootPath.$info[0]['savepath'] . $info[0]['savename'];
            $path = substr($path,8);
            //将图片插入数据库，但在插入之前需要先把之前的头像删除掉
            $user = M('User');
            $oldPath = $user->field('zonedp')->find($uid)['zonedp'];
            @unlink('./Public' . $oldPath);
            $data = array('zonedp' => $path);
            $result = $user->where("id='{$uid}'")->setField($data);
            if ($result > 0) {
                $this->ajaxReturn(array('status' => 0, 'path' => $path,'message'=>'上传成功'));
            } else {
                $this->ajaxReturn(array('status' => 1,'message'=>'上传失败'));
            }


        }
    }

    public function photoUpload()
    {
        $uid = I('session.qzone')['home']['id'];
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/uploads/'; // 设置附件上传根目录
        $upload->subName = array('date', 'Ymd'); // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->ajaxReturn(array('status' => 1));
        } else {// 上传成功
            $path = $upload->rootPath.$info[0]['savepath'] . $info[0]['savename'];
            $path = substr($path,8);
            //如果插入数据库失败则把图片删除
            $album_id = I('get.album_id');
            $model = M('Album');
            if ($model->field('uid')->find($album_id)['uid'] != $uid) {
                @unlink('./Public' . $path);
                $this->ajaxReturn(array('status' => 1, 'message' => '您不是相册的主人，无法在此相册中上传图片！'));
            }
            $modelPhoto = M('AlbumPhoto');
            $data['uid'] = $uid;
            $data['path'] = $path;
            $data['album_id'] = $album_id;
            $data['ctime'] = time();
            $modelPhoto->create($data);
            $result = $modelPhoto->add();
            //照片上传成功，写入动态
            $DModel = M('Dynamic');
            $DData['uid'] = $uid;
            $DData['username'] = $_SESSION['qzone']['home']['username'];
            $DData['zonedp'] = $_SESSION['qzone']['home']['zonedp'];
            //日志的动态内容格式是标题加部分内容
            $DData['content'] = "<p>上传一张照片到相册<a href='".U('Xiangce/photo',array('user_id'=>$uid,'album_id'=>$album_id))."'>".I('post.album_name')."</a></p>"."<img src='/qzone/Public{$path}'/>";
            $DData['is_show'] = 1;
            $DData['ctime'] = time();
            $DModel->create();
            $DModel->add($DData);
            if ($result > 0) {
                $this->ajaxReturn(array('status' => 0, 'path' => $path, 'time' => date('Y-m-d H:i',$data['ctime']),'id'=>$result, 'message' => '上传成功'));
            } else {
                @unlink('./Public' . $path);
                $this->ajaxReturn(array('status' => 1, 'message' => '上传失败'));
            }


        }
    }
}